﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GenIX
{
    public partial class SelectStyleForm : Form
    {
        string currentNameStyle;
        string nameNewStyle;
        MainForm form;
        public SelectStyleForm()
        {
            InitializeComponent();
            if (Styleform.choseStyleName == "") this.Close();
            currentNameStyle = Styleform.choseStyleName;
            nameNewStyle = currentNameStyle;
        }

        private void SelectStyle(object sender, EventArgs e)
        {
            var item = sender as ListBox;
            nameNewStyle = item.SelectedItem.ToString();
            Styleform.choseStyleName = nameNewStyle;
            Styleform.SelectElement(this.form, Styleform.choseStyleName);

        }
        private void Ini()
        {
            if (Styleform.Err == true)
            {
                listBox1.Items.Clear();
                listBox1.Items.AddRange(Styleform.listStyleName);
                listBox1.SelectedItem = Styleform.choseStyleName;
                Styleform.SelectElement(this, Styleform.choseStyleName);
            }
            else
            {
                MessageBox.Show(Styleform.Error(), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }
        private void LoadStyle(object sender, EventArgs e)
        {

            this.form = this.Owner as MainForm;
            if (this.form == null) this.Close();
            Ini();
        }
        private void Close(object sender, FormClosedEventArgs e)
        {
           
            Styleform.choseStyleName = currentNameStyle;
            Styleform.SelectElement(this.form, Styleform.choseStyleName);

        }
        private void SaveActiveStyle(object sender, EventArgs e)
        {
            
            if (currentNameStyle != nameNewStyle)
                Styleform.SetActiveStyle(currentNameStyle, nameNewStyle);
            currentNameStyle = nameNewStyle;
            this.Close();
        }
        private void DellStyle_Click(object sender, EventArgs e)
        {
           
            int count = listBox1.Items.Count;
            if (count > 1)
            {
                string temp = listBox1.SelectedItem.ToString();
                if (temp == "Стандартный") { MessageBox.Show("Cтандартный стиль нельзя редактировать", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; }
                int curItem = listBox1.Items.IndexOf(temp);
                if (curItem == count - 1)
                {
                    listBox1.SelectedItem = listBox1.Items[curItem - 1].ToString();
                    listBox1.Items.Remove(temp);
                    Styleform.SetActiveStyle(temp, listBox1.SelectedItem.ToString());
                    currentNameStyle = listBox1.SelectedItem.ToString();
                    Styleform.Remove(temp);
                }
                else
                {
                    listBox1.SelectedItem = listBox1.Items[curItem + 1].ToString();
                    listBox1.Items.Remove(temp);
                    Styleform.SetActiveStyle(temp, listBox1.SelectedItem.ToString());
                    currentNameStyle = listBox1.SelectedItem.ToString();
                    Styleform.Remove(temp);
                }
            }
            else
                MessageBox.Show("Нельзя удалить последний стиль", "Предуприждение", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        private void AddStyle_Click(object sender, EventArgs e)
        {

            Name newStyle = new Name();
            newStyle.ShowDialog();
            Ini();


        }

       
        
        private void InsertStyle(object sender, EventArgs e)
        {
            NewStyleForm newStyle = new NewStyleForm();
            if (listBox1.SelectedItem == null) return;
            if (listBox1.SelectedItem.ToString() == "Стандартный") { MessageBox.Show("Cтандартный стиль нельзя редактировать", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; }
            newStyle.ChangeStyle(listBox1.SelectedItem.ToString());
            newStyle.ShowDialog();
            nameNewStyle = listBox1.SelectedItem.ToString();
            Styleform.choseStyleName = nameNewStyle;
            Styleform.SelectElement(this.form, Styleform.choseStyleName);


        }

    }
}
